<!DOCTYPE html>
<html>
<head>
    <title>Light Refraction Diagram</title>
</head>
<body>
<canvas id="physicsCanvas" width="600" height="400" style="border:1px solid #d3d3d3;"></canvas>
<script>
    const canvas = document.getElementById("physicsCanvas");
    const ctx = canvas.getContext("2d");

    /**
     * Draws an arrow on the canvas.
     * @param {CanvasRenderingContext2D} ctx - The canvas rendering context.
     * @param {number} fromX - The starting x-coordinate.
     * @param {number} fromY - The starting y-coordinate.
     * @param {number} toX - The ending x-coordinate.
     * @param {number} toY - The ending y-coordinate.
     * @param {string} color - The color of the arrow.
     * @param {number} width - The line width of the arrow.
     */
    function drawArrow(ctx, fromX, fromY, toX, toY, color, width) {
        const headlen = 12;
        const dx = toX - fromX;
        const dy = toY - fromY;
        const angle = Math.atan2(dy, dx);

        ctx.save();
        ctx.strokeStyle = color;
        ctx.fillStyle = color;
        ctx.lineWidth = width;

        // Main line
        ctx.beginPath();
        ctx.moveTo(fromX, fromY);
        ctx.lineTo(toX, toY);
        ctx.stroke();

        // Arrowhead
        ctx.beginPath();
        ctx.moveTo(toX, toY);
        ctx.lineTo(toX - headlen * Math.cos(angle - Math.PI / 6), toY - headlen * Math.sin(angle - Math.PI / 6));
        ctx.lineTo(toX - headlen * Math.cos(angle + Math.PI / 6), toY - headlen * Math.sin(angle + Math.PI / 6));
        ctx.closePath();
        ctx.fill();
        ctx.restore();
    }

    // --- Main Drawing ---

    // Prism coordinates
    const p1 = { x: 50, y: 50 };
    const p2 = { x: 50, y: 350 };
    const p3 = { x: 550, y: 350 };

    // Calculate incidence point on the hypotenuse
    const incY = 200;
    const m = (p3.y - p1.y) / (p3.x - p1.x); // slope of hypotenuse
    const incX = (incY - p1.y) / m + p1.x;
    const incPoint = { x: incX, y: incY };

    // Draw the prism
    ctx.strokeStyle = 'grey';
    ctx.lineWidth = 3;
    ctx.beginPath();
    ctx.moveTo(p1.x, p1.y);
    ctx.lineTo(p2.x, p2.y);
    ctx.lineTo(p3.x, p3.y);
    ctx.closePath();
    ctx.stroke();

    // Draw the "Glass" label
    ctx.fillStyle = 'black';
    ctx.font = '22px Arial';
    ctx.fillText('Glass', 65, 330);

    // Draw the incident ray
    drawArrow(ctx, 580, incPoint.y, incPoint.x, incPoint.y, 'black', 2.5);

    // Define properties for outgoing rays
    const rayColor = '#9F0000'; // Dark red color similar to the image
    const rayWidth = 2.5;

    // Define endpoints for the rays to match the original image's layout
    const endA = { x: 180, y: 140 };
    const endB = { x: 120, y: 200 };
    const endC = { x: 180, y: 260 };
    const endD = { x: 280, y: 340 };
    const endE = { x: 230, y: 340 };

    // Draw rays and their labels
    ctx.font = '22px Arial';

    drawArrow(ctx, incPoint.x, incPoint.y, endA.x, endA.y, rayColor, rayWidth);
    ctx.fillText('A', 170, 130);

    drawArrow(ctx, incPoint.x, incPoint.y, endB.x, endB.y, rayColor, rayWidth);
    ctx.fillText('B', 170, 190);

    drawArrow(ctx, incPoint.x, incPoint.y, endC.x, endC.y, rayColor, rayWidth);
    ctx.fillText('C', 170, 280);

    drawArrow(ctx, incPoint.x, incPoint.y, endD.x, endD.y, rayColor, rayWidth);
    ctx.fillText('D', 285, 330);

    drawArrow(ctx, incPoint.x, incPoint.y, endE.x, endE.y, rayColor, rayWidth);
    ctx.fillText('E', 205, 330);

</script>
</body>
</html>